package org.example.system.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

/**
 * 微信小程序配置类
 */
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "wx")
public class WechatConfig {
    private String appId;
    private String appSecret;


    /**
     * 微信小程序配置
     * @return 微信小程序配置
     */
    @Bean(name = "wxMaConfig")
    public WxMaConfig wxMaConfig(){
        WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
        wxMaConfig.setAppid(appId);
        wxMaConfig.setSecret(appSecret);
        wxMaConfig.setMsgDataFormat(WxMaConstants.MsgDataFormat.JSON);
        return  wxMaConfig;
    }

    /**
     * 微信小程序核心服务组件
     * @param wxMaConfig 微信小程序配置
     * @return 微信小程序核心服务组件
     */
    @Bean(name = "wxMaService")
    @DependsOn(value = {"wxMaConfig"})
    public WxMaService wxMaService(@Qualifier(value = "wxMaConfig") WxMaConfig wxMaConfig){
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(wxMaConfig);
        return wxMaService;
    }
}